from utils.excel_util import ExcelUtil
from model.sysUserModel import SysUser
from schema.sysUserSchema import UserCreate
from typing import List, Any, Dict, ClassVar
from io import BytesIO


class SysUserExcel:
    # 定义映射字典为类变量
    SEX_MAP: ClassVar[Dict[str, str]] = {
        "0": "男",
        "1": "女",
        "2": "未知"
    }

    STATUS_MAP: ClassVar[Dict[str, str]] = {
        "0": "正常",
        "2": "已删除"
    }

    USE_FLAG_MAP: ClassVar[Dict[str, str]] = {
        "0": "正常",
        "1": "禁用"
    }

    # 用户导入导出的列定义
    COLUMNS: ClassVar[List[Dict[str, Any]]] = [
        {"field": "user_name", "title": "用户名", "required": True},
        {"field": "nick_name", "title": "昵称", "required": True},
        {"field": "password", "title": "密码", "required": True},
        {"field": "dept_id", "title": "部门ID", "required": False},
        {"field": "email", "title": "邮箱", "required": False},
        {"field": "phonenumber", "title": "手机号码", "required": False},
        {"field": "sex", "title": "性别", "required": False},
        {"field": "remark", "title": "备注", "required": False}
    ]

    @classmethod
    def _format_sex(cls, value: str) -> str:
        """格式化性别"""
        return cls.SEX_MAP.get(str(value), "未知")

    @classmethod
    def _format_status(cls, value: str) -> str:
        """格式化状态"""
        return cls.STATUS_MAP.get(str(value), "未知")

    @classmethod
    def _format_use_flag(cls, value: str) -> str:
        """格式化使用标志"""
        return cls.USE_FLAG_MAP.get(str(value), "未知")

    # 将 EXPORT_COLUMNS 定义为类方法，这样可以正确引用类变量和方法
    @classmethod
    def get_export_columns(cls) -> List[Dict[str, Any]]:
        return [
            {"field": "user_name", "title": "用户名"},
            {"field": "nick_name", "title": "昵称"},
            {"field": "dept_id", "title": "部门ID"},
            {"field": "email", "title": "邮箱"},
            {"field": "phonenumber", "title": "手机号码"},
            {"field": "sex", "title": "性别", "formatter": cls._format_sex},
            {"field": "status", "title": "状态", "formatter": cls._format_status},
            {"field": "use_flag", "title": "使用状态", "formatter": cls._format_use_flag},
            {"field": "create_time", "title": "创建时间"},
            {"field": "login_ip", "title": "最后登录IP"},
            {"field": "login_date", "title": "最后登录时间"},
            {"field": "remark", "title": "备注"}
        ]

    @classmethod
    def get_import_template(cls) -> BytesIO:
        """获取用户导入模板"""
        example_data = [{
            "user_name": "zhangsan",
            "nick_name": "张三",
            "password": "123456",
            "dept_id": 1,
            "email": "zhangsan@example.com",
            "phonenumber": "13800138000",
            "sex": "0",  # 对应"男"
            "remark": "示例数据"
        }]
        return ExcelUtil.create_import_template(
            columns=cls.COLUMNS,
            example_data=example_data,
            sheet_name="用户导入模板"
        )

    @classmethod
    async def export_users(cls, users: List[Any]) -> BytesIO:
        """导出用户数据"""
        return await ExcelUtil.export_data(
            data=users,
            columns=cls.get_export_columns(),  # 使用方法获取列定义
            sheet_name="用户数据"
        )

    @classmethod
    def parse_import_users(cls, file: BytesIO) -> List[Dict[str, Any]]:
        """解析用户导入数据"""
        return ExcelUtil.parse_import_data(
            file=file,
            columns=cls.COLUMNS,
            schema_cls=UserCreate
        )